﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;
using MyWeb.Data;

public partial class MinaServices_Roles_DanhSachQuyen : System.Web.UI.Page
{
    public static bool Insert;
    tblROLESInfo rolesInfo = new tblROLESInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        try
        {
            grdRoles.DataSource = tblROLESService.tblROLES_GetByAll();
            grdRoles.DataBind();

        }
        catch (Exception)
        {

            WebMsgBox.Show("Không có dữ liệu");
        }

    }
    protected void lblUpdate_Click(object sender, EventArgs e)
    {
        if (Insert)
        {

        }
        else
        {
            #region[TestInput]

            if (txtName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Bạn chưa nhập Tên Quyền!");
                txtName.Focus();
                return;
            }

            #endregion

            var obj = new tblROLESInfo();
            obj.ROLEID = txtRolesID.Text;
            obj.ROLENAME = txtName.Text;
            tblROLESService.tblROLES_Update(obj);
        }
        BindGrid();
        pnShow.Visible = true;
        pnUpdate.Visible = false;
    }
    protected void grdRoles_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        tblROLESInfo rolesInfo = new tblROLESInfo();
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            DataTable dt = tblROLESService.tblROLES_GetById(id);
            txtRolesID.Text = id;
            txtName.Text = dt.Rows[0]["ROLENAME"].ToString();
            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else
        {
            tblROLESService.tblROLES_Delete(id);
            BindGrid();
        }
    }
    protected void grdRoles_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grdRoles.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
   
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/MinaServices/Roles/ThemQuyen.aspx");
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        try
        {
            DataGridItem item = default(DataGridItem);
            for (int i = 0; i < grdRoles.Items.Count; i++)
            {
                item = grdRoles.Items[i];
                if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                {
                    if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                    {
                        tblROLESInfo strId = new tblROLESInfo();

                        strId.ROLEID = item.Cells[1].Text;
                        try
                        {
                            tblROLESService.tblROLES_Delete(strId.ROLEID);
                        }
                        catch
                        {
                            WebMsgBox.Show("Xóa thất bại!");
                        }
                    }
                }
            }
            grdRoles.CurrentPageIndex = 0;
            BindGrid();
        }
        catch
        {
        }
    }
    protected void grdRoles_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        ListItemType itemType = e.Item.ItemType;
        if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
        {
            if (itemType == ListItemType.Header)
            {
                object checkBox = e.Item.FindControl("chkSelectAll");
                if ((checkBox != null))
                {
                    ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                }
            }
            else
            {
                string tableRowId = grdRoles.ClientID + "_row" + e.Item.ItemIndex.ToString();
                e.Item.Attributes.Add("ROLEID", tableRowId);
                object checkBox = e.Item.FindControl("chkSelect");
                if ((checkBox != null))
                {
                    e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                    e.Item.Attributes.Add("onMouseOut",
                                          "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                    ((CheckBox)checkBox).Attributes.Add("onClick",
                                                         "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex +
                                                         ")");
                }
            }
        }
    }
    protected void lblBack_Click(object sender, EventArgs e)
    {
        pnShow.Visible = true;
        pnUpdate.Visible = false;
    }
}